<?php

use Admin\Models\Modul\Step as Step,
    Admin\Models\Modul\Page as Page,
    Admin\Models\User\Role as Role,
    Admin\Models\Video as Video,
    Admin\Models\Navigation\Header as Header,
    Admin\Models\Navigation\Link as Link,
    Admin\Libraries\Menu as Menu,
    Logger as Logger;

/**
 * System Management Modul class
 *
 * @package default
 * @author joharijumali@gmail.com
 **/

class Admin_System_Controller extends Admin_Base_Controller {


    public function get_index(){

    	return View::make('admin::system.index');

    }


    /**
     * Navigation Setup function
     * Component : navigate
     * Method : ajax post
     * @return void
     * @author joharijumali@gmail.com
     **/

    public function get_navigate(){

        $data['render'] = Menu::navTree();
        $data['headerlist'] = $list = Header::listheader();
        // $data['page'] = $listpage = Page::listpages();
        $data['pagelist'] = $listpage = Video::listAvailvideo();

        return View::make('admin::system.navigate',$data);

    }

    public function post_navigate()
    {

        $contents = Input::get();

        foreach ($contents['module'] as $key => $value) {
            $module = Header::find($value);
            $module->step  = $key+1;
            $module->save();
        }

        foreach ($contents['parent'] as $key => $value) {
            $module = Link::find($value);
            $module->parentstep  = $key+1;
            $module->save();
        }

        Log::write('System', 'Update Navigation by '.Auth::user()->username);

        return Redirect::to('admin/system/navigate');
    }


    /**
     * navigation utilities function
     *
     * @return json data
     * 
     **/

    public function get_navchild(){

        $input = Input::get();

        $navigation = Link::find($input['navid']);
        $nav = Header::find($navigation->navheaderid);
        $parent = Page::find($navigation->modulpageid);

        $data['navheaderid'] = $navigation->navheaderid;
        $data['module'] = $nav->navheader;
        $data['parentid'] = $navigation->navpageid;
        $data['parent'] = $parent->actionalias;

        return json_encode($data);
    }

    public function get_resetnavdata(){

        $data['navheaderid'] = $list = Header::listheader();
        $data['modulpageid'] = $listpage = Video::listAvailvideo();

        return json_encode($data);

    }

    public function get_upload(){

        $data['render'] = Video::listAllVideo();

        return View::make('admin::system.upload',$data);

    }


    public function post_upload(){
        
        $input = Input::all();

        $extension = File::extension($input['file']['name']);
        $directory = 'video/'.str_replace("-", "", date('Y-m-d')); 
        $filename = Str::random(16, 'alpha').time().".{$extension}";

        if(!is_dir(path('public').$directory)){
            mkdir(path('public').$directory, 0777,true);
        }

        $maxSize = ini_get('upload_max_filesize')*1024*1024*1024;

        if($input['file']['size'] != null && $input['file']['size'] < $maxSize){
            try{

                $upload_success = Input::upload('file', path('public').$directory, $filename);

                if($upload_success){
                    $input['vidPath'] = $directory.'/'.$filename;
                    unset($input['file']);

                    Video::uploadVideo($input);
                }

            }catch (Exception $e) {
                Log::write('upload', 'Upload error: ' . $e->getMessage());
            }
        }else{
            Log::write('upload', 'Upload error: Exceed max size '.ini_get('upload_max_filesize'));
        }
        //     $upload_success = Input::upload('photo', $directory, $filename);

        //     if( $upload_success ) {

        //         while(is_file('public/'.$profile->imgpath) == TRUE)
        //         {
        //             chmod('public/'.$profile->imgpath, 0666);
        //             unlink('public/'.$profile->imgpath);
        //         }

        //         $profile->imgpath = 'avatar/'.sha1(Auth::user()->userid).'/'.$filename;
        //     }
        // }

        // Log::write('System', 'Add New Navigation Module '.$input['navheader'].' by '.Auth::user()->username);

        return Redirect::to('admin/system/upload');
    }

    public function post_setmodule(){
        
        $input = Input::get();

        // if($input['roleid'] == NULL):
        $module = new Header;
        // else:
        //  $role = Admin_UserRole::find($input['roleid']);
        // endif;
        $module->navheader  = Str::title($input['navheader']);
        $module->save();

        Log::write('System', 'Add New Module '.$input['navheader'].' by '.Auth::user()->username);

        return Menu::navTree();
    }

    public function post_setpage(){
        
        $input = Input::get();

        $pages = new Link;

        $pages->navheaderid  = $input['navheaderid'];
        $pages->modulpageid  = $input['modulpageid'];
        $pages->save();


        $header = Header::find($input['navheaderid'])->navheader;
        $page = Video::find($input['modulpageid'])->vidtitle;

        Log::write('System', 'Add New Video '.$header.' / '.$page.' by '.Auth::user()->username);

        return Menu::navTree();
    }

    public function post_setchild(){

        $input = Input::get();

                // if($input['roleid'] == NULL):
        $pages = new Link;
        // else:
        //  $role = Admin_UserRole::find($input['roleid']);
        // endif;
        $pages->navheaderid  = $input['navheaderid'];
        $pages->parentid  = $input['parentid'];
        $pages->modulpageid  = $input['modulpageid'];
        $pages->save();

        $header = Header::find($input['navheaderid'])->navheader;
        $parent = Video::find($input['parentid'])->vidtitle;
        $subpage = Video::find($input['modulpageid'])->vidtitle;

        Log::write('System', 'Add New Navigation Sub Page '.$header.'/'.$parent.'/'.$subpage.' by '.Auth::user()->username);

        return Menu::navTree();
    }

    public function post_deletepages(){
        
        $input = Input::get();

        $desc = Link::find($input['id'])->modulpage()->first();

        Link::find($input['id'])->delete();

        Log::write('System', 'Delete Navigation Page '.$desc->actionalias.' by '.Auth::user()->username);

        return Menu::navTree();
    }

    public function post_deletemodule(){
        
        $input = Input::get();

        $header = Header::find($input['id']);

        $head = $header->navheader;

        $header->delete();

        $desc = Link::where('navheaderid','=',$input['id'])->get();

        Link::where('navheaderid','=',$input['id'])->delete();
        
        Log::write('System', 'Delete Navigation Module '.$head.' by '.Auth::user()->username);

        foreach ($desc as $key => $value) {
            $page = Video::find($input['modulpageid'])->vidtitle;
            Log::write('System', 'Delete Navigation Page '.$head.'/'.$page.' by '.Auth::user()->username);
        }

        return Menu::navTree();
    }


    /**
     * System Logger function
     *
     * @return void
     * 
     **/

    public function get_logger(){

    	// $files = Logger::get_logs();

    	// foreach ($files as $key => $content) {
    	// 	$logs = basename($content,'.log');
    	// 	$data['files'][$logs] = Logger::get_log($logs);
    	// }

        $logs = Logger::get_log(date('Y-m-d'));
        $data['currentselection'] = date('d-m-Y');
        $data['logs'] = !empty($logs) ? $logs : array('No Log Data Recorded');
    	
    	return View::make('admin::system.logs',$data);

    }

    public function post_logger(){

        $input = Input::get();

        $logs = Logger::get_log(date('Y-m-d',strtotime($input['date'])));

        $data['currentselection'] = date('d-m-Y',strtotime($input['date']));
        $data['logs'] = !empty($logs) ? $logs : array('No Log Data Recorded');

        return View::make('admin::system.logs',$data);
    }


}